package com.kukudb.asaproject.serveice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kukudb.asaproject.pojo.UserLicenses;
import com.kukudb.asaproject.repository.UserLicensesRepository;
import com.kukudb.asaproject.serveice.interfaceTemp.UserLicensesInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserLicensesImpl implements UserLicensesInterface {
    Logger logger = LoggerFactory.getLogger(UserLicensesImpl.class);
    @Autowired
    UserLicensesRepository userLicensesRepository;
    @Override
    public UserLicenses getUserLicenses(Integer userId) {
        logger.info("查询用户{}的许可信息");
        UserLicenses userLicenses = userLicensesRepository.selectOne(new QueryWrapper<UserLicenses>().eq("user_id", userId));
//        if (userLicenses == null) {
//            throw new RuntimeException("用户" + userId + "没有授权");
//        }
        return userLicenses;
    }

    @Override
    public boolean getPermissions(UserLicenses userLicenses) {
        logger.info("检查用户{}的许可信息",userLicenses);
        if("1".equals(userLicenses.getStatus())) {
            if ("1".equals(userLicenses.getLicenseType())) {
                if (!(userLicenses.getEndTime().getTime() < new Date().getTime())) {
                    return true;
                }
            } else if ("2".equals(userLicenses.getLicenseType())) {
                if (userLicenses.getUsedValue() < userLicenses.getTotalValue()) {
                    return true;
                }
            }
        }
        return false;
    }
}
